package com.zh.mediaservice.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zh.mediacommon.constant.MessageConstant;
import com.zh.mediacommon.ecxeption.NotFoundException;
import com.zh.mediacommon.result.PageResult;
import com.zh.mediacommon.util.ProvinceUtil;
import com.zh.mediapojo.dto.HospitalPageDto;
import com.zh.mediapojo.entity.Department;
import com.zh.mediapojo.entity.Doctor;
import com.zh.mediapojo.entity.Hospital;
import com.zh.mediapojo.vo.HospitalPageVo;
import com.zh.mediapojo.vo.HospitalVo;
import com.zh.mediaservice.mapper.DepartmentMapper;
import com.zh.mediaservice.mapper.DoctorMapper;
import com.zh.mediaservice.mapper.HospitalMapper;
import com.zh.mediaservice.service.HospitalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class HospitalServiceImpl implements HospitalService {
    private static final Logger log = LoggerFactory.getLogger(HospitalServiceImpl.class);
    @Autowired
    HospitalMapper hospitalMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Override
    public List<Hospital> getAllHospital() {
        List<Hospital> hospitalList = hospitalMapper.getAllHospital();
        return hospitalList;
    }

    @Override
    public PageResult page(HospitalPageDto hospitalPageDto) {
        PageHelper.startPage(hospitalPageDto.getPage(),hospitalPageDto.getPageSize());
        log.info("hospitalPageDto {}",hospitalPageDto);
//        if(hospitalPageDto.getProvince()!=null) {
//            hospitalPageDto.setProvince(ProvinceUtil.getProvince(hospitalPageDto.getProvince()));
//        }
            Page<HospitalPageVo> page = hospitalMapper.page(hospitalPageDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

@Override
public HospitalVo getHospitalById(Integer id) {
    Hospital hospital = hospitalMapper.getHospitalById(id);
    if (hospital==null)
        throw new NotFoundException("id错误,不存在该数据");
    HospitalVo hospitalVo = new HospitalVo();
    hospitalVo.setHospital(hospital);
    List<Department> departmentList = departmentMapper.getDepartmentByHospitalId(id);
    log.info("departmentList {}", departmentList);
    hospitalVo.setDepartments(departmentList);
    return hospitalVo;
}

@Override
public PageResult searchHospital(Integer page, Integer pageSize, String keyWords) {
    PageHelper.startPage(page,pageSize);
    Page<HospitalPageVo> searchPage=hospitalMapper.searchHospital(keyWords);
    return new PageResult(searchPage.getTotal(),searchPage.getResult());
}

@Override
public List<Doctor> getDoctorBySid(Integer sid) {
    List<Doctor> doctorList = doctorMapper.getDoctorBySid(sid);
    if((doctorList==null || doctorList.size()==0))
        throw new NotFoundException(MessageConstant.NOT_FIND_DOCTOR);
    return doctorList;
}

}
